# delete_tab.pl
# function for tabform

# load modules
require "./electric_bulletin_board-lib.pl";
require "./config.pl";


# delete_tab()
# delete tab generate functions
sub delete_tab() {
	# configure form start
	print &ui_form_start("index.cgi", "get");
	
	# tab name
	print &ui_hidden("tab", "delete");
	
	# pattern list
	print "<h4>パターン一覧</h4>";
	
	my %filelist = ();
	&read_file("$module_root_directory/pattern/file.list", \%filelist);
	my @filelist_key = keys(%filelist);
	
	foreach my $key (@filelist_key) {
		my $date = $key;
		$date =~ s/\..*$//;
		
		$filelist{$key} = &serial2time($date) . " $filelist{$key}";
	}
	
	my @filelist_value = values(%filelist);
	print &ui_select("deletepattern", undef, \@filelist_value, 10, 1);
	print &item_br(2);
	
	# delete check
	print "<h4>選択されたパターンを削除しますか？</h4>";
	print &ui_yesno_radio("deletecheck", $in{'deletecheck'} || 0);
	
	# run button
	print &item_hr();
	print &ui_submit("削除");
	
	# configure form end
	print &ui_form_end();
}

# delete_run()
# delete run functions
sub delete_run() {
	# delete process
	if ($in{'deletecheck'} eq "") {
		return;
	}
	
	# check delete pattern
	if ($in{'deletepattern'} eq "") {
		&error("パターンが選択されていません");
	}
	
	# delete check
	if ($in{'deletecheck'} eq "1") {
		
		# search file name from file.list
		my @deletepattern = split(/\0/, $in{'deletepattern'});
		my $match_filename_array_ref;
		$match_filename_array_ref = &search_from_file("$module_root_directory/pattern/file.list", \@selectpattern);
		foreach my $filename (@$match_filename_array_ref) {
			
			# delete file
			&unlink_file("$module_root_directory/pattern/$key");
			
			# change file.list
			my %filelist_change = ();
			&read_file("$module_root_directory/pattern/file.list", \%filelist_change);
			delete $filelist_change{$key};
			&write_file("$module_root_directory/pattern/file.list", \%filelist_change);
		}
		
		&error("complete delete file");
	} elsif ($in{'deletecheck'} eq "0") {
		&error("error 削除する場合は「はい」を選択してから「削除」をクリックしてください");
	}
	print &item_br(2);
	
}

